#include "cabinetconfig.h"
#include <QApplication>
#include <QDebug>
#include <QSettings>

CabinetConfig* CabinetConfig::c = new CabinetConfig();

CabinetConfig::CabinetConfig(QObject *parent) : QObject(parent)
{
    curUser = NULL;
}

CabinetConfig *CabinetConfig::config()
{
    return c;
}

void CabinetConfig::configInit()
{
    appPath = qApp->applicationDirPath();
    configPath = appPath+"/cabinet.ini";
    userPath = appPath+"/user.ini";
    qDebug()<<"apppath:"<<appPath<<configPath;
}

QList<UserInfo *> CabinetConfig::getUsers()
{
    QSettings settings(userPath, QSettings::IniFormat);
    QStringList userList = settings.childGroups();
    QList<UserInfo*> users;

    foreach(QString user, userList)
    {
        QString name = settings.value(user+"/name",QString()).toString();
        QString post = settings.value(user+"/post",QString()).toString();
        QString id = settings.value(user+"/id",QString()).toString();
        QString card = settings.value(user+"/card",QString()).toString();
        users<<new UserInfo(name,post,id,card);
    }
    return users;
}

void CabinetConfig::addUser(UserInfo *info)
{
    QSettings settings(userPath, QSettings::IniFormat);
    QString user = info->id;
    settings.setValue(user+"/name", info->name);
    settings.setValue(user+"/post", info->post);
    settings.setValue(user+"/id", info->id);
    settings.setValue(user+"/card", info->card);
    settings.sync();
}

void CabinetConfig::removeUser(UserInfo *info)
{
    QSettings settings(userPath, QSettings::IniFormat);
    QString user = info->id;
    settings.remove(user);
    settings.sync();
}

void CabinetConfig::setScreenPos(QPoint pos)
{
    setConfig("/base/ScreenPos", QString("%1 %2").arg(pos.x()).arg(pos.y()));
}

void CabinetConfig::setLayout(int seq, QString lay)
{
    QString path = QString("Cabinet%1/layout").arg(seq);
    setConfig(path, lay);
}

void CabinetConfig::setCabinetNum(int num)
{
    setConfig("/base/CabNum", num);
}

void CabinetConfig::setCtrlSeq(int col, QByteArray ctrlWd)
{
    qDebug()<<"[setCtrlSeq]"<<ctrlWd;
    QString path = QString("Cabinet%1/ctrlSeq").arg(col);
    setConfig(path, ctrlWd);
}

void CabinetConfig::setCtrlIo(int col, QByteArray ctrlWd)
{
    QString path = QString("Cabinet%1/ctrlIo").arg(col);
    setConfig(path, ctrlWd);
}

void CabinetConfig::setTexts(int col, QStringList texts)
{
    qDebug()<<"[setTexts]"<<col<<"size:"<<texts.count();
    QString path = QString("Cabinet%1/texts").arg(col);
    setConfig(path, texts);
}

void CabinetConfig::setLockDev(QString devName)
{
    qDebug()<<"[setLockDev]"<<devName;
    setConfig("base/devName", devName);
}

void CabinetConfig::setLockBaudrate(int baudrate)
{
    qDebug()<<"[setLockBaudrate]"<<baudrate;
    setConfig("base/devBaudrate", baudrate);
}

void CabinetConfig::setCabinetId(QString cabId)
{
    QString path = QString("/base/CabinetId");
    setConfig(path, cabId);
}

void CabinetConfig::setDepartName(QString depName)
{
    QString path = QString("/base/DepartName");
    setConfig(path, depName);
}

void CabinetConfig::setDepartId(QString depId)
{
    QString path = QString("/base/DepartId");
    setConfig(path, depId);
}

void CabinetConfig::setApiAddress(QString address)
{
    QString path = QString("/base/ApiAddress");
    setConfig(path, address);
}

void CabinetConfig::setCtrlAddress(QString address)
{
    QString path = QString("/base/CtrlAddress");
    setConfig(path, address);
}

void CabinetConfig::setCurUser(UserInfo *info)
{
    curUser = info;
}

void CabinetConfig::setIp(QString ip)
{
    QSettings settings("/home/config/network.ini", QSettings::IniFormat);
    settings.setValue("ip", ip);
    settings.sync();
}

void CabinetConfig::setGateway(QString gateway)
{
    QSettings settings("/home/config/network.ini", QSettings::IniFormat);
    settings.setValue("gateway", gateway);
    settings.sync();
}

void CabinetConfig::setMac(QString mac)
{
    QSettings settings("/home/config/network.ini", QSettings::IniFormat);
    settings.setValue("mac", mac);
    settings.sync();
}

void CabinetConfig::setNetmask(QString mask)
{
    QSettings settings("/home/config/network.ini", QSettings::IniFormat);
    settings.setValue("netmask", mask);
    settings.sync();
}

void CabinetConfig::setDevice()
{
    QSettings settings("/home/config/network.ini", QSettings::IniFormat);
    settings.setValue("device", "eth0");
    settings.sync();
}

void CabinetConfig::setSpecialCase(int col, int row)
{
    setConfig("/base/SpecialCase", QString("%1 %2").arg(col).arg(row));
}

QPoint CabinetConfig::getScreenPos()
{
    QVariant v = getConfig("/base/ScreenPos");
    if(v.isNull())
        return QPoint(0,0);
    QStringList listP = v.toString().split(" ", QString::SkipEmptyParts);
    if(listP.count() != 2)
        return QPoint(0,0);

    bool ok;
    QPoint ret;

    ret.setX(listP[0].toInt(&ok));
    if(!ok)
        return QPoint(0,0);

    ret.setY(listP[1].toInt(&ok));
    if(!ok)
        return QPoint(0,0);

    return ret;
}

QStringList CabinetConfig::getAllLayout()
{
    int cabNum = getConfig("/base/CabNum").toInt();
    QStringList ret;

    for(int i=0; i<cabNum; i++)
    {
        ret<<getConfig(QString("Cabinet%1/layout").arg(i)).toString();
    }

    return ret;
}

QByteArray CabinetConfig::getCtrlSeq(int col)
{
    QString path = QString("Cabinet%1/ctrlSeq").arg(col);
    QString pathLay = QString("Cabinet%1/layout").arg(col);
    int cabSize = getConfig(pathLay).toString().length();
    QByteArray ret = getConfig(path, QByteArray(cabSize, '\0')).toByteArray();
//    qDebug()<<"cabSize"<<cabSize<<ret;
    return ret;
}

QByteArray CabinetConfig::getCtrlIo(int col)
{
    QString path = QString("Cabinet%1/ctrlIo").arg(col);
    QString pathLay = QString("Cabinet%1/layout").arg(col);
    int cabSize = getConfig(pathLay).toString().length();
    return getConfig(path,QByteArray(cabSize, '\0')).toByteArray();
}

QStringList CabinetConfig::getTexts(int col)
{
    QString path = QString("Cabinet%1/texts").arg(col);
    return getConfig(path).toStringList();
}

QString CabinetConfig::getLockDev()
{
    QString path = QString("base/devName");
    return getConfig(path).toString();
}

int CabinetConfig::getLockBaudrate()
{
    QString path = QString("base/devBaudrate");
    return getConfig(path).toInt();
}

QString CabinetConfig::getCabinetId()
{
    QString path = QString("base/CabinetId");
    return getConfig(path).toString();
}

QPoint CabinetConfig::getSpecialCase()
{
    QVariant v = getConfig("/base/SpecialCase");
    if(v.isNull())
        return QPoint(0,0);
    QStringList listP = v.toString().split(" ", QString::SkipEmptyParts);
    if(listP.count() != 2)
        return QPoint(0,0);

    bool ok;
    QPoint ret;

    ret.setX(listP[0].toInt(&ok));
    if(!ok)
        return QPoint(0,0);

    ret.setY(listP[1].toInt(&ok));
    if(!ok)
        return QPoint(0,0);

    return ret;
}

QString CabinetConfig::getDepartName()
{
    QString path = QString("base/DepartName");
    return getConfig(path).toString();
}

QString CabinetConfig::getDepartId()
{
    QString path = QString("base/DepartId");
    return getConfig(path, QString()).toString();
}

QString CabinetConfig::getApiAddress()
{
    QString path = QString("/base/ApiAddress");
    QString ret = getConfig(path).toString();
    if(ret.isEmpty())
        ret = QString("http://192.168.3.81:1001/uapws/service/receiveservice");
//        ret = QString("http://hereyou.imwork.net:9091/uapws/service/receiveservice");
    return ret;
}

QString CabinetConfig::getCtrlAddress()
{
    QString path = QString("/base/CtrlAddress");
    QString ret = getConfig(path, QString()).toString();
    if(ret.isEmpty())
        ret = QString("http://192.168.4.152:8086/SpdService/barCodeForIntelligentCabinet");
//        ret = QString("http://lanto.wicp.net:54171/SpdService/barCodeForIntelligentCabinet");
//        ret = QString("http://hereyou.imwork.net:9091/uapws/service/receiveservice");
    return ret;
}

UserInfo *CabinetConfig::getCurUser()
{
    return curUser;
}

QString CabinetConfig::getIp()
{
    QSettings settings("/home/config/network.ini", QSettings::IniFormat);
    return settings.value("ip", "172.17.17.17").toString();
}

QString CabinetConfig::getGateway()
{
    QSettings settings("/home/config/network.ini", QSettings::IniFormat);
    return settings.value("gateway", "172.17.17.1").toString();
}

void CabinetConfig::removeCabinet(int seq)
{
    QString path = QString("Cabinet%1").arg(seq);
    removeConfig(path);
}

void CabinetConfig::setConfig(QString key, QVariant value)
{
    QSettings settings(configPath, QSettings::IniFormat);
    settings.setValue(key, value);
    settings.sync();
}

QVariant CabinetConfig::getConfig(QString key, QVariant defaultRet)
{
    QSettings settings(configPath, QSettings::IniFormat);
    return settings.value(key, defaultRet);
}

void CabinetConfig::removeConfig(QString path)
{
    QSettings settings(configPath, QSettings::IniFormat);
    settings.remove(path);
}
